package com.frxuexi.park2302.fragment

import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.filter
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.mvi.BaseFragment
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.LiveDataBus
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.FragmentHomeBinding
import com.frxuexi.park2302.notice.NoticeIntent
import com.frxuexi.park2302.notice.NoticePageAdapter
import com.frxuexi.park2302.notice.NoticeViewModel
import com.frxuexi.park2302.notice.OnItemClickListener
import com.frxuexi.park2302.notice.OnItemLongClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding, NoticeIntent, NoticeViewModel>() {
    override val binding: FragmentHomeBinding by lazy { FragmentHomeBinding.inflate(layoutInflater) }
    override val vm: NoticeViewModel by viewModels<NoticeViewModel>()
    val noticePageAdapter by lazy { NoticePageAdapter() }
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when(it.type){
                            RequestType.DELETE->{

                            }
                            else->{
                                val data = it.data as PagingData<NoticeBeanItem>
                                noticePageAdapter.submitData(data.filter {
                                    it.title.isNotEmpty()
                                })
                            }
                        }

                    }

                    is UiState.onError -> {
                        Log.i("lr", it.msg)
                    }

                    else -> {}
                }
            }
        }

    }

    override fun initView() {
//        EventBus.getDefault().post("消息来了")

        LiveDataBus.with<String>("msg").observe(this){
            ToastUtils.showLong(it)
        }

        binding.homeHeader.apply {
            headBack.setImageResource(R.drawable.ic_action_account)
            headTitle.visibility = View.GONE
            headNotice.visibility = View.VISIBLE
            headAdd.setImageResource(R.drawable.ic_action_account)
        }
        binding.rvNotice.apply {
            adapter=noticePageAdapter
            layoutManager=LinearLayoutManager(requireContext())
        }

        noticePageAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int, bean: NoticeBeanItem) {
                Log.i("===","${position}---${bean}")
            }

        })

        noticePageAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(position: Int): Boolean {
                ToastUtils.showLong("${position}")
                return true
            }

        })

        vm.sendIntent(NoticeIntent.getNotice(1, 10))

        binding.llSign.setOnClickListener {
            ARouter.getInstance().build("/sign/SignAddActivity").navigation()
        }
    }



}